package com.blb.ssh.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.blb.ssh.entity.Menu;
import com.blb.ssh.service.MenuService;
import com.opensymphony.xwork2.ActionSupport;

public class MenuAction extends ActionSupport{
	
		private MenuService menuService;
		private Menu menu;
		private String menuIds;
		private int page;
		
		private int rows;
		
		private List<Menu> menuList = new ArrayList<Menu>();
		private List<Menu> menuTypeList = new ArrayList<Menu>();
		private Map<String, Object> resultMap;
		private List<Map<String, Object>> menuData;
		
		/**
		 * 通过登录的用户的角色Id获取相关的菜单信息
		 * 
		 */
		public String getMenuTreeByRoleId(){
			HttpSession session = ServletActionContext.getRequest().getSession();
			int roleId = (Integer)session.getAttribute("Roleid");
			menuData = new ArrayList<Map<String, Object>>();  
			//获取角色ID对应的菜单列表
			List<Menu> hasMenuList = menuService.getMenuDataByRoleId(roleId);
			//通过循环来获取第一层的所有节点
			List<Menu> lv1MenuList = new ArrayList<Menu>();
			for(Menu lv1menu: hasMenuList){
				int pid = lv1menu.getMenuPid();
				if(pid == 0){
					lv1MenuList.add(lv1menu);
				}
			}
			
			for(Menu menu: lv1MenuList){
				Map<String, Object> menuMap = new HashMap<String, Object>();
				menuMap.put("id", menu.getMenuId());
				menuMap.put("text", menu.getMenuName());
				menuMap.put("pid", menu.getMenuPid());
				menuMap.put("url", menu.getMenuUrl());
				getChildNodesByRole(menuService, menu.getMenuId(), menuMap, hasMenuList);
				
				menuData.add(menuMap);
			}
			return "menuTree";
		}
		
		/**
		 * 递归获取菜单列表
		 * @param menuService
		 * @param currentHeroId
		 * @param parentMap
		 * @param childList
		 */
		public void getChildNodesByRole(MenuService menuService, int currentId, Map<String, Object> parentMap, List<Menu> allNodeList){
			//下一级节点的所有数据
			List<Menu> currentLvList = new ArrayList<Menu>();
			//遍历角色已经的所有菜单,如果菜单的父ID等于上一级节点的ID则,放入list
			for(Menu currentMenu: allNodeList){
				int pid = currentMenu.getMenuPid();
				if(currentId == pid){
					currentLvList.add(currentMenu);
				}
			}
			
			//如果有则进行遍历
			if(currentLvList.size() > 0){
				List<Map<String, Object>> nextLvList = new ArrayList<Map<String, Object>>();
				for(Menu lvMenu: currentLvList){
					Map<String, Object> menulvMap = new HashMap<String, Object>();
					int currentLvId = lvMenu.getMenuId();
					menulvMap.put("id", currentLvId);
					menulvMap.put("text", lvMenu.getMenuName());
					menulvMap.put("pid", lvMenu.getMenuPid());
					menulvMap.put("url", lvMenu.getMenuUrl());
					//getChildNodesByRole(menuService, currentLvId, menulvMap, allNodeList);
					
					nextLvList.add(menulvMap);
				}
				parentMap.put("children", nextLvList);
				parentMap.put("state", "closed");
			}else{
				parentMap.put("state", "open");
			}
		
		}
		
		
		/**
		 * 通过登录的用户的角色Id获取相关的菜单信息对应的权限
		 * 
		 */
		public String getMenuAll(){
			HttpSession session = ServletActionContext.getRequest().getSession();
			//获取角色ID对应的菜单列表
			List<Menu> MenuList = menuService.getMenuAll(page,rows);
			//获取菜单条数
			Long menuCount=menuService.getMenuCount();
			resultMap = new HashMap<String,Object>();
			resultMap.put("total", menuCount);
			resultMap.put("rows", MenuList);
			
			return "data";
		}
		
		/**
		 * 添加权限
		 * @return
		 */
		public String addMenu() {
			resultMap = new HashMap<String,Object>();
			resultMap.put("isok", menuService.addMenu(menu));
			return "data";
			
		}
		
		/**
		 * 删除权限
		 * @return
		 */
		public String batchDeleteMenu(){
			resultMap = new HashMap<String,Object>();
			resultMap.put("isok", menuService.batchDeleteMenu(menuIds));
			return "data";
		}
		
		/**
		 * 修改用户
		 * @return
		 */
		public String editMenu(){
			resultMap = new HashMap<String,Object>();
			resultMap.put("isok", menuService.editMenu(menu));
			return "data";
		}
		
		/**
		 * 查询全部
		 * @return
		 */
		public String selectAllmenuPidList() {
			
			menuList = menuService.getMenuPidAll();
			
			return "list";
		}
		
		/**
		 * 查询权限类型
		 * @return
		 */
		public String selectAllmenuTypeList() {
			
			menuTypeList = menuService.selectAllmenuTypeList();
			
			return "typelist";
		}
		
		public List<Menu> getMenuList() {
			return menuList;
		}
		public void setMenuList(List<Menu> menuList) {
			this.menuList = menuList;
		}



		public List<Map<String, Object>> getMenuData() {
			return menuData;
		}
		public void setMenuData(List<Map<String, Object>> menuData) {
			this.menuData = menuData;
		}
		public Map<String, Object> getResultMap() {
			return resultMap;
		}
		public void setResultMap(Map<String, Object> resultMap) {
			this.resultMap = resultMap;
		}
		public MenuService getMenuService() {
			return menuService;
		}

		public void setMenuService(MenuService menuService) {
			this.menuService = menuService;
		}

		public Menu getMenu() {
			return menu;
		}

		public void setMenu(Menu menu) {
			this.menu = menu;
		}

		public String getMenuIds() {
			return menuIds;
		}

		public void setMenuIds(String menuIds) {
			this.menuIds = menuIds;
		}

		public int getPage() {
			return page;
		}

		public void setPage(int page) {
			this.page = page;
		}

		public int getRows() {
			return rows;
		}

		public void setRows(int rows) {
			this.rows = rows;
		}

		public List<Menu> getMenuTypeList() {
			return menuTypeList;
		}

		public void setMenuTypeList(List<Menu> menuTypeList) {
			this.menuTypeList = menuTypeList;
		}
		
		
		
	

}
